package com.weareint.biz.service.impl;

import com.weareint.biz.vo.TUserVo;
import com.weareint.biz.search.TUserSearch;
import com.weareint.biz.converter.TUserConverter;
import com.weareint.biz.repository.TUserRepository;
import com.weareint.biz.entity.TUser;
import com.weareint.biz.entity.QTUser;
import com.weareint.biz.service.ITUserService;
import com.weareint.common.jpabase.QueryCondition;
import com.weareint.common.jpabase.ConditionService;
import com.weareint.common.exception.BizException;
import com.weareint.common.exception.ExceptionEnum;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.dsl.StringTemplate;
import com.querydsl.core.types.dsl.Expressions;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.PageImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;



/**
 * <pre>
 *      
 * </pre>
 *
 * @author Johnny
 * @date 2024-04-20
 */
@Service
@Transactional(rollbackFor = {Exception.class, RuntimeException.class, Error.class})
@Slf4j
public class TUserServiceImpl implements ITUserService,ConditionService<TUser> {

    private final JPAQueryFactory queryFactory;

    private final TUserRepository tUserRepository;

    public TUserServiceImpl(JPAQueryFactory queryFactory, TUserRepository tUserRepository) {
        this.queryFactory = queryFactory;
        this.tUserRepository = tUserRepository;
    }

    private QTUser qTUser = QTUser.tUser;

    /**
     * 带条件查询的 分页接口
     *
     * @param queryCondition : 查询条件=TUserSearch
     * @param pageable : pageable
     * @return : Page<TUserVo>
     */
    @Override
    public Page<TUserVo> listByCondition(QueryCondition queryCondition, Pageable pageable) {
        List<TUserVo> list = new ArrayList<>();

        JPAQuery<TUser> jpaQuery = queryFactory.select(qTUser)
            .from(qTUser)
            .offset(pageable.getPageNumber() * pageable.getPageSize())
            .limit(pageable.getPageSize());

        //添加 查询条件
        fillCondition(jpaQuery, queryCondition);

        QueryResults<TUser> queryResults = jpaQuery.fetchResults();

        queryResults.getResults().forEach(tUser -> {
             TUserVo tUserVo = TUserConverter.INSTANCE.domain2vo(tUser);
             list.add(tUserVo);
        });
        return new PageImpl<>(list, pageable, queryResults.getTotal());
    }
    /**
     * 新增接口
     *
     * @param tUserVo : 新增对象
     */
    @Override
    public void add(TUserVo tUserVo) {
        tUserVo.setId(null);
        TUser tUser = TUserConverter.INSTANCE.vo2domain(tUserVo);
        tUserRepository.save(tUser);
    }
    /**
     * 更新接口
     *
     * @param tUserVo : 更新对象
     */
    @Override
    public void edit(TUserVo tUserVo) {
        if (tUserRepository.existsById(tUserVo.getId())) {
            TUser tUser = TUserConverter.INSTANCE.vo2domain(tUserVo);
            tUserRepository.save(tUser);
        } else {
            throw new BizException(ExceptionEnum.NOT_FIND_BY_ID);
        }
    }
    /**
     * 删除接口
     *
     * @param id : 删除对象的Id
     */
    @Override
    public void delete(Long id) {
        if (tUserRepository.existsById(id)) {
            tUserRepository.deleteById(id);
        } else {
            throw new BizException(ExceptionEnum.NOT_FIND_BY_ID);
        }
    }
    /**
    * 批量删除接口
    *
    * @param ids : 批量删除对象
    */
    @Override
    public void deletes(List<Long> ids) {
        tUserRepository.deleteByIdIn(ids);
    }
    /**
     * 填充 查询条件
     *
     * @param jpaQuery : jpaQuery
     * @param queryCondition : 查询条件
     */
    @Override
    public void fillCondition(JPAQuery<TUser> jpaQuery, QueryCondition queryCondition) {
        TUserSearch tUserSearch = (TUserSearch) queryCondition;
        if (StringUtils.isNotEmpty(tUserSearch.getUserId())) {
            jpaQuery.where(qTUser.userId.eq(tUserSearch.getUserId()));
        }
        if (StringUtils.isNotEmpty(tUserSearch.getUsername())) {
            jpaQuery.where(qTUser.username.eq(tUserSearch.getUsername()));
        }
        if (StringUtils.isNotEmpty(tUserSearch.getPassword())) {
            jpaQuery.where(qTUser.password.eq(tUserSearch.getPassword()));
        }
    }


}
